{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 1: Definition and Use\n",
    "\n",
    "What does the following program print? Why?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pressure(value):\n",
    "    print('pressure is', str(value))\n",
    "    \n",
    "print(pressure, 22.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What do you think the intent behind this program was, and how would you fix the code to match it?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pressure(22.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Change the Names\n",
    "\n",
    "Fill in the blanks to create a function that takes a name like \"Rochelle Terman\" and returns that name in uppercase and reversed, like \"TERMAN, ROCHELLE\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def long_function(name_string):\n",
    "    upper_case = name_string.upper() # make upper\n",
    "    upper_case_list = upper_case.split() # turn into a list\n",
    "    first_name = upper_case_list[0] # take first name\n",
    "    last_name = upper_case_list[1] # take last name\n",
    "    reversed_name = last_name + \", \" + first_name # reverse and separate by a comma and space\n",
    "    return(reversed_name)\n",
    "\n",
    "long_function(\"John Doe\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In one line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def long_function(name_string):\n",
    "    return ', '.join(name_string.upper().split()[::-1])\n",
    "\n",
    "\n",
    "long_function(\"Rochelle Terman\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 3: Order of Operations\n",
    "\n",
    "See what the following programs prints"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_date(year, month, day):\n",
    "    joined = str(year) + '/' + str(month) + '/' + str(day)\n",
    "    print(joined)\n",
    "\n",
    "result = print_date(1871, 3, 19)\n",
    "print('result of call is:', result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Explain why the two lines of output appeared in the order they did."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer\n",
    "\n",
    "The date was printed first because the `print_date` function actually prints the date, and it was called first. Since there was no `return` statement in the function declaration, the default value of `None` was returned and assigned to the `result` variable. Then, the `print` function was called using the `result` variable, which contains `None`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 4: Calling by Name\n",
    "\n",
    "What does this short program print?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_date(year, month, day):\n",
    "    joined = str(year) + '/' + str(month) + '/' + str(day)\n",
    "    print(joined)\n",
    "\n",
    "print_date(day=1, month=2, year=2003)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When and why is it useful to call functions this way?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer\n",
    "\n",
    "This short function prints the date in the format \"year/month/day\", and thus the cell above will print the string `2003/2/1` because the arguments passed into the function are explicitly stated (e.g. `day=1`). This is a bit of foreshadowing for the next notebook wherein we'll see how to use this syntax to provide optional parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 5: Find the substring\n",
    "\n",
    "In our project, we'll be dealing with texts like this (from Djibouti's 2013 review):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rec_1 = \"143.1 Pursue efforts to ratify international human rights instruments (Kuwait);\" \n",
    "rec_2 = \"143.2 Ratify the international human rights instruments to which Djibouti is not yet party (Niger);\" \n",
    "rec_3 = \"143.3 Carry on with the ratification of international conventions (Democratic Republic of Congo);\" \n",
    "rec_4 = \"143.4 Speed up measures aimed at ratifying the Optional Protocol to CEDAW (Republic of Moldova);\" "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you see, the recommending country is always at the end of the line in parentheses.\n",
    "\n",
    "Write a function that accepts a recommendation (a string) and returns the recommending country (also a string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_country(rec):\n",
    "    pieces = rec.split(\"(\")\n",
    "    end = pieces[-1]\n",
    "    country = end[:-2]\n",
    "    return country\n",
    "\n",
    "get_country(rec_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In one line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_country(rec):\n",
    "    return rec.split(\"(\")[-1][:-2]\n",
    "\n",
    "get_country(rec_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Modify the function above to return a [`tuple`](http://www.tutorialspoint.com/python/python_tuples.htm) with element 0 being the country name, and element 1 being the recommendation as a string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_country_rec(rec):\n",
    "    pieces = rec.split(\"(\")\n",
    "    end = pieces[-1]\n",
    "    front = pieces[0]\n",
    "    country = end[:-2]\n",
    "    \n",
    "    front_words = front.split()[1:]\n",
    "    front_rec = ' '.join(front_words)\n",
    "\n",
    "    return (country, front_rec)\n",
    "\n",
    "country_and_rec = get_country_rec(rec_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(country_and_rec[0])\n",
    "print(country_and_rec[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In one line--**DON'T EVER WRITE CODE LIKE THIS**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_country_rec(rec):\n",
    "    return (rec.split(\"(\")[-1][:-2], ' '.join(rec.split(\"(\")[0].split()[1:]))\n",
    "\n",
    "get_country_rec(rec_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
